<?php
class Fn
{
    var $area;

    static function areaFromAddr($addr, $city)
    {
        if (is_array($addr)) {
            $addr = join("", $addr);
        }
        if (preg_match("/青秀区/", $addr)) {
            $area = "青秀区";
        }
        if (preg_match("/江南区/", $addr)) {
            $area = "江南区";
        }
        if (preg_match("/西乡塘区?/", $addr)) {
            $area = "西乡塘区";
        }
        if (preg_match("/兴宁区?/", $addr)) {
            $area = "兴宁区";
        }
        if (preg_match("/良庆区?/", $addr)) {
            $area = "良庆区";
        }
        if ($area == "" && preg_match("/(?:市|路|省|^)?(.*区)/U", $addr, $nn)) {
            $area = $nn[1];
        }
        return $area;
    }

    static function streetFromAddr($addr, $city)
    {
        if (is_array($addr)) {
            $addr = join("", $addr);
        }
        preg_match("/^(?:.*(?:区|市))??(.*(大道|路|街))/Uisx", $addr, $nn);
        $street = $nn[1];
        return $street;
    }

    static function varstoarray($arr)
    {
        $newarr = array();
        foreach ($arr as $k => $v) {
            foreach ($v as $k2 => $v2) {
                if (!is_int($v2)) $newarr[] = strip_tags($v2);
            }
        }
        return $newarr;
    }

    static function changeArray($arr)
    {
        $newarr = array();
        if(!empty($arr)){
        foreach ($arr as $name => $onearr) {
            foreach ($onearr as $index => $value) {
                $newarr[$index][$name] = $value;
            }
        }
        }
        return $newarr;
    }

    static function join($arr)
    {
        $return = join(' ', $arr);
        $return = trim($return);
        return $return;
    }

    static function arrformao($arr, $keyarray)
    {
        $narr = array();
        if (!empty($arr))
            foreach ($arr as $k => $v) {
                foreach ($keyarray as $k2 => $v2) {
                    if (in_array($v2, $keyarray)) {
                        $narr[$k][$v2] = $v->$v2;
                    }
                }
            }
        return $narr;
    }

    static function arrformaa($arr, $key)
    {
        $arr = array();
        foreach ($arr as $k => $v) {
            $arr[] = $v[$k];
        }
        return $arr;
    }

    static function arrformoa($arr, $key)
    {
        $arr = array();
        foreach ($object as $k => $v) {
            $arr[] = $object->$k;
        }
        return $arr;
    }

    static function arrforma($arr, $key)
    {
        $arr = array();
        foreach ($object as $k => $v) {
            $arr[] = $object->$k;
        }
        return $arr;
    }

    static function arrformo($object, $key)
    {
        $arr = array();
        foreach ($object as $k => $v) {
            $arr[] = $object->$k;
        }
        return $arr;
    }

    static function lian($str, $add, $qian = 0)
    {
        if ($qian) {
            $str = ($str != "") ? $add . trim($str) : $str;
        } else {
            $str = ($str != "") ? trim($str) . $add : $str;
        }
        return $str;
    }

    static function arrkeyrename($arr, $keyarr)
    {
        $oldkey = array();
        if (!empty($arr)) {
            foreach ($arr as $k2 => $v2) {
                foreach ($v2 as $keyname => $value2) {
                    $oldkey[] = $keyname;
                }
                break;
            }
        }
        $narr = array();
        if (!empty($arr)) {
            foreach ($arr as $k => $v) {
                for ($i = 0; $i < count($keyarr); $i++) {
                    $narr[$k][$keyarr[$i]] = $arr[$k][$oldkey[$i]];
                }
            }
        }
        return $narr;
    }

    static function baidu2google($lat, $lng)
    {
        $return = curl2("http://gpsspg.com/ajax/latlng_office.aspx?lat=22.8195119060&lng=108.3558752940&type=2 ");
        $data = json_decode($return);
        $return = $data->google->lat . "," . $data->google->lng;
        return $return;
    }

    static function baidu2gps($lat, $lng)
    {
        $return = curl2("http://gpsspg.com/ajax/latlng_office.aspx?lat=22.8195119060&lng=108.3558752940&type=2 ");
        $data = json_decode($return);
        $return = $data->gps->lat . "," . $data->gps->lng;
        return $return;
    }

    static function u8($str)
    {
        $string = "";
        if (is_array($str)) {
            foreach ($str as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        $string = $string . Fn::str_to_unicode_word($v2, '') . ",";
                    }
                } else {
                    $string = $string . Fn::str_to_unicode_word($v, '') . ",";
                }

            }
            $string = substr($string, 0, -1);
        } else {
            $string = Fn::str_to_unicode_word($str, '');
        }

        return $string;
    }

    static function str_to_unicode_word($str, $depart = ' ')
    {
        $arr = array();
        $str_len = mb_strlen($str, 'utf-8');
        for ($i = 0; $i < $str_len; $i++) {
            $s = mb_substr($str, $i, 1, 'utf-8');
            if ($s != ' ' && $s != '　') {
                $arr[] = 'ux' . Fn::utf8_to_unicode($s);
            }
        }
        return implode($depart, $arr);
    }

//分词
    static function fenchi($str)
    {
        require_once "d:/sogxoto/system/libs/words.php";
        $words = words::segment($str);
        $words[] = $str;
        return $words;
    }

    function msubstr($str, $start = 0, $length = 15, $charset = "utf-8", $suffix = true)
    {
        if (function_exists("mb_substr")) {
            $slice = mb_substr($str, $start, $length, $charset);
            if ($suffix & $slice != $str) return $slice . "…";
            return $slice;
        } elseif (function_exists('iconv_substr')) {
            return iconv_substr($str, $start, $length, $charset);
        }
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
        if ($suffix && $slice != $str) return $slice . "…";
        return $slice;
    }

    //字符编码转换

    function iconv($in_charset, $out_charset, $str)
    {
        require 'libs/iconv.php';
        $chinese = new Chinese();
        return $chinese->Convert($in_charset, $out_charset, $str);
    }


    //JSON兼容
    function json_encode($data)
    {
        require_once 'libs/json.php';
        $JSON = new JSON();
        return $JSON->encode($data);
    }


    function json_decode($data)
    {
        require_once 'libs/json.php';
        $JSON = new JSON();
        return $JSON->decode($data, 1);
    }

    function utf8_to_unicode($char)
    {
        switch (strlen($char)) {
            case 1:
                return ord($char);
            case 2:
                $n = (ord($char[0]) & 0x3f) << 6;
                $n += ord($char[1]) & 0x3f;
                return $n;
            case 3:
                $n = (ord($char[0]) & 0x1f) << 12;
                $n += (ord($char[1]) & 0x3f) << 6;
                $n += ord($char[2]) & 0x3f;
                return $n;
            case 4:
                $n = (ord($char[0]) & 0x0f) << 18;
                $n += (ord($char[1]) & 0x3f) << 12;
                $n += (ord($char[2]) & 0x3f) << 6;
                $n += ord($char[3]) & 0x3f;
                return $n;
        }
    }

    static function match($str)
    {
        $fc = Fn::fenchi($str);
        return Fn::u8($fc);
    }

    static function isnull($E)
    {
        if (is_string($E)) {
            $rt = (trim($E) == "") ? true : false;
        } else {
            if (empty($E)) {
                $rt = true;
            } else {
                $rt = false;
            }
        }
        return $rt;
    }

    static function arraytoaimage($imgarr)
    {
        if (is_string($imgarr)) {
            return $imgarr;
        } else if (is_array($imgarr)) {
            return $imgarr[0];
        }
    }

    static function in_arrays($a, $array)
    {
        if (is_array($array)) {
            return in_array($a, $array);
        } else {
            return false;
        }
    }

    static function sjson_decode($str)
    {
        preg_match_all("/\((\{.*\})\)/isx", $str, $nn);
        $str = $nn[1][0];
        return json_decode($str);
    }

    static function gethostjc($url)
    {
        $nn = parse_url($url);
        $host = $nn['host'];
        preg_match("/(?:[a-z0-9-_]*\.)*([a-z0-9-_]+)\.(?:com\.cn|com|cn|org|gov)/", $host, $nn);
        $hostjc = $nn[1];
        $hostjc = (preg_match("/[0-9]/", (substr($hostjc, 0, 1)))) ? "u" . $hostjc : $hostjc;
        $hostjc = ucfirst($hostjc);
        return $hostjc;
    }

    static function pingjia($str)
    {
        $str = (is_float($str)) ? intval($str) : $str;
        switch ($str) {
            case "极差":
            case "1":
                $return = 1;
                break;
            case "差":
            case "2":
                $return = 2;
                break;
            case "一般":
            case "3":
                $return = 3;
                break;
            case "很好":
            case "良好":
            case "良":
            case "4":
                $return = 4;
                break;
            case "非常好":
            case "5":
                $return = 5;
                break;
        }
        return $return;
    }

    function parste_time($str0)
    {
        if (preg_match("/([0-9]{2,4})[\-\/]([0-9]{1,2})[\-\/]([0-9]{1,2})\s*(?:([0-9]{1,2})\:([0-9]{1,2})(?:\:([0-9]{1,2}))?)?/isx", $str0, $nn)) {
            $str = str_pad($nn[1], 4, "20", STR_PAD_LEFT) . "-" . str_pad($nn[2], 2, "0", STR_PAD_LEFT) . "-" . str_pad($nn[3], 2, "0", STR_PAD_LEFT) . " " . str_pad($nn[4], 2, "0", STR_PAD_LEFT) . ":" . str_pad($nn[5], 2, "0", STR_PAD_LEFT) . ":" . str_pad($nn[6], 2, "0", STR_PAD_LEFT);
            //date("Y-m-d H:i:s",mktime(substr($str, 11, 2), substr($str, 14, 2), substr($str, 17, 2), substr($str, 5, 2), substr($str, 8, 2), substr($str, 0, 4)))
            return mktime(substr($str, 11, 2), substr($str, 14, 2), substr($str, 17, 2), substr($str, 5, 2), substr($str, 8, 2), substr($str, 0, 4));
        }else if(is_int($str0)){
            return $str0;
        }

        return time();
    }

    function class2array($class){
        $arr=array();
        foreach($class as $k=>$v){
            if(is_object($v)){
                $arr[$k]=class2array($v);
            }else{
                $arr[$k]=$v;
            }
        }
        return $arr;
    }

    function firststring($o){
        if(is_array($o)){
            if(is_array(current($o))){
                return firststring(current($o));
            }else{
                return current($o);
            }
        }else{
            return $o;
        }
    }

    function clln($text){
        $text="　　".preg_replace("/\r?\n\s*/","\r\n<p>　　",trim($text));
        $text=preg_replace("/。/","。\r\n<p>　　",$text);
        return $text;
    }

    function write($nr){
        $fn=fopen("d:/1.txt","w");fwrite($fn,$nr);fclose($fn);
    }

    function print_r($nr){
        $fn=fopen("d:/1.txt","w");fwrite($fn,print_r($nr,1));fclose($fn);
    }

    function arr2html($arr,$htm){
        $s="";
        if(is_array($arr)&&!empty($arr)){
            foreach($arr as $k=>$v){
                $temp=preg_replace("/\{\\\$nn\[1\]\}/",$k,$htm);
                $temp=preg_replace("/\{\\\$nn\[2\]\}/",$v,$temp);
                $s.=$temp;
            }
            return $s;
        }else{
            return $arr;
        }
    }

    function findOther($key,$other)
    {
        foreach ($other as $k=>$v) {
            if (is_string($other[$k][$key]) && trim($other[$k][$key]) != "") {
                return $other[$k][$key];
            } else if (!empty($other[$k][$key])) {
                return $other[$k][$key];
            }
        }
        return "";
    }

    function findOtherArray($key,$other)
    {
        foreach ($other as $k=>$v) {
            if (!empty($other[$k][$key])) {
                return $other[$k][$key];
            }
        }
        return "";
    }

    function clwz($str){
       $str=preg_replace("/详情|\[|\]/","",$str);
        return $str;
    }

    function baidumappoint($address){
        $address=urlencode($address);
        $url="http://api.map.baidu.com/geocoder/v2/?address={$address}&output=json&ak=B47e6934c8717810a4f651f6fa301339&callback=showLocation";
        $nr=curl2($url);
        preg_match_all("/\"lng\"\s*\:\s*([0-9\.]+),\"lat\"\s*\:\s*([0-9\.]+)\}/Uisx",$nr,$nn);
        return array($nn[1][0],$nn[2][0]);
    }

    function jingwei($address)
    {
        $address=preg_replace("/\(|\)|（|）|\-/"," ",$address);
        $address=(preg_match("/市|县|乡/",$address)?$address:"南宁市".$address);
        $address = urlencode($address);
        $url = "http://api.map.baidu.com/geocoder/v2/?address={$address}&output=json&ak=B47e6934c8717810a4f651f6fa301339&callback=showLocation";
        $nr = curl2($url);
        preg_match_all("/\"lng\"\s*\:\s*([0-9\.]+),\"lat\"\s*\:\s*([0-9\.]+)\}/Uisx", $nr, $nn);
        return $nn[1][0] . "," . $nn[2][0];
    }

    function getonebyurl($url){
        $classname=self::gethostjc($url);
        if(is_file("class/{$classname}.class.php")){
            include_once("myclass/{$classname}.class.php");
            $o=new $classname();
            $data=$o->getByUrl($url);
        }
        return $data;
    }

    function samp_vars($data,$other){
        $arr=array("title","name","address","mobile","tel","alias","tags","detail","province_name","city","area","street","category","images","is_effect","opentime","tese","line","fw","qifen","shopdesc","tag","lat","lng","price","zone","siteinfo","dianpingarr","good_dp_count","bad_dp_count","avg_point","common_dp_count","total_point","image_count","reg_avg_pice","good_rate","common_rate","shoppic","website","yurl");
        for($i=0;$i<count($arr);$i++){
            //if($data[$arr[$i]]==""||is_array($data[$arr[$i]])&&empty($data[$arr[$i]])){
                for($j=0;$j<count($other);$j++){
                    if(count($other[$j])<1) continue;
                    if($other[$j][$arr[$i]]!=""||is_array($other[$j][$arr[$i]])&&empty($other[$j][$arr[$i]])){
                        if(is_string($other[$j][$arr[$i]])){
                            $data[$arr[$i]]=$other[$j][$arr[$i]];
                            break;
                        }else if(is_array($other[$j][$arr[$i]])){
                            $data[$arr[$i]]=empty($data[$arr[$i]])?array():$data[$arr[$i]];
                            $data[$arr[$i]]=array_merge($data[$arr[$i]],$other[$j][$arr[$i]]);
                        }
                    }
                }
            //}
        }
        return $data;
    }
}

?>